﻿namespace UsefulTools.ExtensionMethods
{
    using System;

    public static class Throw
    {
        /// <summary>
        /// Throws an InvalidOperationException if the condition is true.
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="message"></param>
        /// <param name="parameterName"></param>
        public static void If(bool condition, string message=null, string parameterName=null)
        {
            if(message == null)
            {
                message = "The condition is false.";
            }
            if (condition)
            {
                throw new ArgumentException(message);
            }
        }
        /// <summary>
        /// Throws an ArgumentNullException if the <paramref name="parameter"/> is null.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameter"></param>
        /// <param name="parameterName"></param>
        public static void IfNull<T>(T parameter, string parameterName = null ) where T : class
        {
            if (parameterName == null)
            {
                parameterName = "";
            }
            if (parameter == null)
            {
                throw new ArgumentNullException("Parameter {0} cannot be null".Formatted(parameterName));
            }
        }
    }
}